package com.ym.consumer.listen;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RabbitMQListen {
    //@RabbitListener(queues = "queueTest")
    //public void test(String string){
    //    log.info(string);
    //}
    //@RabbitListener(queues = "workQueue")
    //public void workQueue1(String string){
    //    log.info("work1,"+string);
    //}
    //@RabbitListener(queues = "workQueue")
    //public void workQueue2(String string) throws InterruptedException {
    //    TimeUnit.MILLISECONDS.sleep(100);
    //    log.warn("work2,"+string);
    //}
    //@RabbitListener(queues = "fanout.queue1")
    //public void fanoutQueue1(String string){
    //    log.info("fanout1,"+string);
    //}
    //@RabbitListener(queues = "fanout.queue2")
    //public void fanoutQueue2(String string){
    //    log.warn("fanout2,"+string);
    //}
    //@RabbitListener(queues = "direct.queue1")
    //public void directQueue1(String string){
    //    log.info("direct1,"+string);
    //}
    //@RabbitListener(queues = "direct.queue2")
    //public void directQueue2(String string){
    //    log.warn("direct2,"+string);
    //}
    //@RabbitListener(queues = "topic.queue1")
    //public void topicQueue1(String string){
    //    log.info("topic1,"+string);
    //}
    //@RabbitListener(queues = "topic.queue2")
    //public void topicQueue2(String string){
    //    log.warn("topic2,"+string);
    //}

    //非常重要
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue3"),
            exchange = @Exchange(name = "direct.exchange"),
            key = {"red","green"}
    ))
    public void directQueue3(String string){
        log.warn("direct3,"+string);
    }
    //@RabbitListener(queues = "objectQueue")
    //public void testJackson(Map<String, Object> map){
    //    log.info("object的类型为"+map.toString());
    //}
    //lazyQueue非常重要
    @RabbitListener(queuesToDeclare = @Queue(
            name = "lazy.queue",
            durable = "true",
            arguments = @Argument(
                    name = "x-queue-mode",
                    value = "lazy"
            )
    ))
    public void testLazy(String string){
        log.info(string);
    }
    //
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue"),
            exchange = @Exchange(name = "delay.exchange",delayed = "true"),
            key = "delay"
    ))
    public void testDelay(String string){
        log.info(String.valueOf(new Date()));
        log.info(string);
    }
    @RabbitListener(queues = "dead.queue")
    public void testDead(String string){
        log.info(String.valueOf(Timestamp.valueOf(LocalDateTime.now())));
        log.info(string);
    }
}
